package online.hxstar.learn.p_http.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.ReadTimeoutHandler;
import online.hxstar.learn.p_http.NettyConstant;
import online.hxstar.learn.p_http.codec.NettyMessageDecoder;
import online.hxstar.learn.p_http.codec.NettyMessageEncoder;
import online.hxstar.learn.p_http.HeartBeatReqHandler;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author huxing
 * @date 2018-12-19 14:02
 */
public class NettyClient {
    private ScheduledExecutorService executor = Executors.
            newScheduledThreadPool(1);

    public static void main(String[] args) throws InterruptedException {
        new NettyClient().connect(NettyConstant.PORT, NettyConstant.REMOTEIP);
    }

    private EventLoopGroup group = new NioEventLoopGroup();

    public void connect(int port, String host) throws InterruptedException {

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(
                                    new NettyMessageDecoder(1024 * 1024, 4, 4));
                            ch.pipeline().addLast("MessageEncoder",
                                    new NettyMessageEncoder());
                            ch.pipeline().addLast("ReadTimeoutHandler",
                                    new ReadTimeoutHandler(50));
                            ch.pipeline().addLast("LoginAuthHandler",
                                    new LoginAuthReqHandler());
                            ch.pipeline().addLast("HeartBeatHandler",
                                    new HeartBeatReqHandler());
                        }
                    });
            ChannelFuture future = bootstrap.connect(
                    new InetSocketAddress(host, port)
                    , new InetSocketAddress(NettyConstant.LOCALIP
                            , NettyConstant.LOCAL_PORT)).sync();
            future.channel().closeFuture().sync();
        } finally {
            executor.execute(() -> {
                try {
                    TimeUnit.SECONDS.sleep(5);
                    //发起重连操作
                    connect(NettyConstant.PORT
                            , NettyConstant.REMOTEIP);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}
